//default for 2569... maybe other Digital pins can be used?
#define DATAOUT 51//MOSI
#define DATAIN  50//MISO 
#define SPICLOCK  52//sck
#define SLAVESELECT 53//ss

//opcodes
//Don't know if these are standard or change for different USB devices...
#define WREN  6
#define WRDI  4
#define RDSR  5
#define WRSR  1
#define READ  3
#define WRITE 2

byte controller_output_data;
byte controller_input_data=0;
byte clr;
int address=0;



char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission. SPIF is the mask bit, END OF TRANSMISSION
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  Serial.begin(9600);

  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  // SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 rate (fastest)
  //Once again, I don't know what SPCR should be for the game pad. These values are from the examble Dig Pot in the Arduino Library. We might have to guess n check
  SPCR = (1<<SPE)|(1<<MSTR);
  clr=SPSR;
  clr=SPDR;
  delay(10);
}

byte read_controller(int controller_address)
{
  //READ controller
  int data;
  digitalWrite(SLAVESELECT,LOW);
  spi_transfer(READ); //transmit read opcode
  spi_transfer((char)(controller_address>>8));   //send MSByte address first
  spi_transfer((char)(controller_address));      //send LSByte address
  data = spi_transfer(0x10); //get data byte
  digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
  return data;
}

void loop()
{
  controller_output_data = read_controller(address);
  Serial.print('here comes some data');
  Serial.print(controller_output_data,DEC);
  Serial.print('\n');
  Serial.print(address);
  address++;
  Serial.print(address);
  if (address == 16)
     address = 0;
    Serial.write('end of index');

  delay(5); //pause for readability
}
                  


